﻿

// JScript File
///Datasheet

function RowCount(row) {
    var id = "ctl00_ContentPlaceHolder3_" + row;
    var tr_obj = document.getElementById(id);
    var RowInsert = tr_obj.rowIndex;
    document.getElementById("ctl00_ContentPlaceHolder3_hihInsertRow").value = RowInsert;
}
function reload() {
    //var pageset_id = document.getElementById("ctl00_ContentPlaceHolder3_hihpagesetid").value;
    //location.href ="Datasheet.aspx?pgs_id="+pageset_id;
    location.href = location.href;
}
function Delete(row) {
    var row_id = row.split('_');
    if (row_id[2] != "")
        var af_row_id = row_id[2];
    var id = "ctl00_ContentPlaceHolder3_" + row;
    var tr_obj = document.getElementById(id);
    var delete_row = tr_obj.rowIndex;
    document.getElementById("ctl00_ContentPlaceHolder3_hihDelete").value = delete_row + "-" + af_row_id;
    return;
}
function GetData(af_row_id, row_id, ctr_id, bind_id, ControlHtml, Isprim) {
    var isValid = true;
    $("img[src$='Images/star.gif']").each(function () {
        if ($(this).prev("input").val() == "") {
            //            if ($("#ctl00_ContentPlaceHolder3_lblMessage").html() == "") {
            $("#ctl00_ContentPlaceHolder3_lblMessage").html("<div class=\"mbErrorMessage\" style=\"width:100%;\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;Provide value for required field(s).</div>");
            $("#ctl00_ContentPlaceHolder3_lblMessage").slideDown("fast"); //.delay(5000).slideUp("fast");
            //            }
            //            else
            //                $("#ctl00_ContentPlaceHolder3_lblMessage").slideDown("fast").delay(5000).slideUp("fast");
            isValid = false;
        }
    });
    if (!isValid) return;

    if (document.getElementById("ctl00_ContentPlaceHolder3_hf_IsNewRow_" + af_row_id).value == "true") {
        document.getElementById("ctl00_ContentPlaceHolder3_hf_IsNewRow_" + af_row_id).value = "false";
        var inputSerialize = $("tr[id=ctl00_ContentPlaceHolder3_tr_assign_" + af_row_id + "] input, select, textarea").serialize();
        inputSerialize = inputSerialize.replace(/ctl00%24ContentPlaceHolder3%24/g, "");
        $.ajax({
            url: "Datasheet.aspx?pgs_id=" + document.getElementById("ctl00_ContentPlaceHolder3_hihpagesetid").value + "&action=InsertNewRecord&NewRowID=" + af_row_id + "&data=&" + inputSerialize + "&val=",
            dataType: "text",
            cache: false,
            beforeSend: function () { },
            success: function (msg) {
                if (msg == "success")
                    document.getElementById("ctl00_ContentPlaceHolder3_hf_IsNewRow_" + af_row_id).value = "false";
                else if (msg != "") {
                    document.getElementById("ctl00_ContentPlaceHolder3_hf_IsNewRow_" + af_row_id).value = "true";
                    document.getElementById("ctl00_ContentPlaceHolder3_lblMessage").innerHTML = "<div class=\"mbMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;" + msg + "</div>";
                }
            },
            complete: function () { },
            error: function (msg) {
                document.getElementById("ctl00_ContentPlaceHolder3_hf_IsNewRow_" + af_row_id).value = "true";
                document.getElementById("ctl00_ContentPlaceHolder3_lblMessage").innerHTML = "<div class=\"mbMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;Enter the value is required fields.</div>";
            }
        });
        return;
    }

    //  var exist=document.getElementById("ctl00_ContentPlaceHolder3_hihexistingIds").value;
    //  var arr_exist=exist.split(',');
    document.getElementById("ctl00_ContentPlaceHolder3_hihUpdate").value = "";
    var control = document.getElementById("ctl00_ContentPlaceHolder3_" + row_id);
    var ctr_value;
    if (control.type == "checkbox") {
        if (control.checked == true)
            ctr_value = true;
        else
            ctr_value = false;
    }
    else {
        //        if(CurrValue!="")
        //        ctr_value=CurrValue;
        //        else
        ctr_value = escape(control.value);
    }
    //  for(i=0;i<arr_exist.length;i++)
    //  {
    var update
    if (bind_id != "")
        update = af_row_id + "|" + ctr_id + "|" + ctr_value + "|" + bind_id + "|" + ControlHtml + "|" + Isprim;
    else
        update = af_row_id + "|" + ctr_id + "|" + ctr_value + "|Nobind|" + ControlHtml + "|" + Isprim; ;

    //  if(af_row_id==arr_exist[i])
    //  {
    document.getElementById("ctl00_ContentPlaceHolder3_hihUpdate").value += update;
    //  }
    //  }
    Save();
}
var randomnumber = Math.floor(Math.random() * 1001);

//window.onload = function Save()
function Save() {

    randomnumber++;
    var xmlHttp;
    var update = document.getElementById("ctl00_ContentPlaceHolder3_hihUpdate").value;
    var rowid = document.getElementById("ctl00_ContentPlaceHolder3_hihrowids").value;
    var pageset_id = document.getElementById("ctl00_ContentPlaceHolder3_hihpagesetid").value;
    var exist = document.getElementById("ctl00_ContentPlaceHolder3_hihexistingIds").value;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                //alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 1) {

        }
        if (xmlHttp.readyState == 4) {
            document.getElementById("ctl00_ContentPlaceHolder3_hihUpdate").value = "";
            //document.getElementById("ctl00_ContentPlaceHolder3_hihrowids").value="";
            var resText = xmlHttp.responseText;

            if (resText != 'null') {
                document.getElementById("ctl00_ContentPlaceHolder3_lblMessage").innerHTML = xmlHttp.responseText;
            }
            else
                document.getElementById("ctl00_ContentPlaceHolder3_lblMessage").innerHTML = "";
        }
    }
    // xmlHttp.open("GET","Data_test.aspx?try="+randomnumber + "&updateMode="+update + "&ps_id=" + pageset_id,true);
    xmlHttp.open("GET", "Datasheet.aspx?ps_id=" + pageset_id + "&try=" + randomnumber + "&updateMode=" + update, true);
    xmlHttp.setRequestHeader('Accept', 'message')
    xmlHttp.send(null);
}

function GetCurrency(HConId, ConId, bret) {
    var breturn = new Boolean(bret);
    var control = document.getElementById("ctl00_ContentPlaceHolder3_" + ConId);
    var hcontrol = document.getElementById("ctl00_ContentPlaceHolder3_" + HConId);
    if (breturn == true)
        control.value = hcontrol.value;
    else
        hcontrol.value = control.value;
}
//Hoodtech Processing Button
function showSave(arg) {
    try {
        if (arg == true) {
            if (document.getElementById("ctl00_ContentPlaceHolder3_lnk_More") != null)
                document.getElementById("ctl00_ContentPlaceHolder3_lnk_More").innerHTML = "&nbsp;<img src='Images/load.gif' border='0'></img>&nbsp;Loading data...";
        }
    }
    catch (ex) {
    }
}

function Navigate(ConId, rowindex) {
    var ControlID = "ctl00_ContentPlaceHolder3_" + ConId;
    var table = document.getElementById('ctl00_ContentPlaceHolder3_datasheet');
    var row = table.rows[rowindex];
    var cell;
    var child;
    var Next;
    var NextChild = new Boolean(false);
    for (cell = 0; cell < row.cells.length; cell++) {
        for (child = 0; child < row.cells[cell].childNodes.length; child++)
            if (row.cells[cell].childNodes[child].id == ControlID) {
                if (row.cells[cell].childNodes[child + 1] != null) {
                    NextChild = true;
                    row.cells[cell].childNodes[child + 1].focus();
                }
                else
                    Next = cell;
                break;
            }
    }
    if (NextChild == false) {
        if (row.cells[Next + 1].childNodes[0] != null) {
            if (row.cells[Next + 1].childNodes[0].type == 'hidden') {
                row.cells[Next + 2].childNodes[0].focus();
            }
            else
                row.cells[Next + 1].childNodes[0].focus();
        }
    }
}

function getPageViewType(obj) {
    try {
        if (obj.value.toUpperCase() != "DS") {
            window.location.href = window.location.href.toLowerCase().replace("datasheet.aspx", "displaygrid.aspx") + "&pgtype=" + obj.value;
            return false;
        }
        else
            return true;
    }
    catch (e) { }
}



